/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.company.management.controller;

import java.io.Serializable;

import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.company.management.entity.EmployeeInfo;
import org.company.management.entity.EmployeeGroup;
import org.company.management.entity.EmployeeSchedule;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.company.management.controller.exceptions.IllegalOrphanException;
import org.company.management.controller.exceptions.NonexistentEntityException;
import org.company.management.entity.Employee;

/**
 *
 * @author Paner
 */
@SuppressWarnings("serial")
public class EmployeeJpaController implements Serializable {

    public EmployeeJpaController() {
    	emf = Persistence.createEntityManagerFactory("Management");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Employee employee) {
        if (employee.getEmployeeScheduleList() == null) {
            employee.setEmployeeScheduleList(new ArrayList<EmployeeSchedule>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            EmployeeInfo employeeInfo = employee.getEmployeeInfo();
            if (employeeInfo != null) {
                employeeInfo = em.getReference(employeeInfo.getClass(), employeeInfo.getId());
                employee.setEmployeeInfo(employeeInfo);
            }
            EmployeeGroup groupid = employee.getGroupid();
            if (groupid != null) {
                groupid = em.getReference(groupid.getClass(), groupid.getId());
                employee.setGroupid(groupid);
            }
            List<EmployeeSchedule> attachedEmployeeScheduleList = new ArrayList<EmployeeSchedule>();
            for (EmployeeSchedule employeeScheduleListEmployeeScheduleToAttach : employee.getEmployeeScheduleList()) {
                employeeScheduleListEmployeeScheduleToAttach = em.getReference(employeeScheduleListEmployeeScheduleToAttach.getClass(), employeeScheduleListEmployeeScheduleToAttach.getId());
                attachedEmployeeScheduleList.add(employeeScheduleListEmployeeScheduleToAttach);
            }
            employee.setEmployeeScheduleList(attachedEmployeeScheduleList);
            em.persist(employee);
            if (employeeInfo != null) {
                Employee oldEmployeeidOfEmployeeInfo = employeeInfo.getEmployeeid();
                if (oldEmployeeidOfEmployeeInfo != null) {
                    oldEmployeeidOfEmployeeInfo.setEmployeeInfo(null);
                    oldEmployeeidOfEmployeeInfo = em.merge(oldEmployeeidOfEmployeeInfo);
                }
                employeeInfo.setEmployeeid(employee);
                employeeInfo = em.merge(employeeInfo);
            }
            if (groupid != null) {
                groupid.getEmployeeList().add(employee);
                groupid = em.merge(groupid);
            }
            for (EmployeeSchedule employeeScheduleListEmployeeSchedule : employee.getEmployeeScheduleList()) {
                Employee oldEmployeeidOfEmployeeScheduleListEmployeeSchedule = employeeScheduleListEmployeeSchedule.getEmployeeid();
                employeeScheduleListEmployeeSchedule.setEmployeeid(employee);
                employeeScheduleListEmployeeSchedule = em.merge(employeeScheduleListEmployeeSchedule);
                if (oldEmployeeidOfEmployeeScheduleListEmployeeSchedule != null) {
                    oldEmployeeidOfEmployeeScheduleListEmployeeSchedule.getEmployeeScheduleList().remove(employeeScheduleListEmployeeSchedule);
                    oldEmployeeidOfEmployeeScheduleListEmployeeSchedule = em.merge(oldEmployeeidOfEmployeeScheduleListEmployeeSchedule);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Employee employee) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Employee persistentEmployee = em.find(Employee.class, employee.getId());
            EmployeeInfo employeeInfoOld = persistentEmployee.getEmployeeInfo();
            EmployeeInfo employeeInfoNew = employee.getEmployeeInfo();
            EmployeeGroup groupidOld = persistentEmployee.getGroupid();
            EmployeeGroup groupidNew = employee.getGroupid();
            List<EmployeeSchedule> employeeScheduleListOld = persistentEmployee.getEmployeeScheduleList();
            List<EmployeeSchedule> employeeScheduleListNew = employee.getEmployeeScheduleList();
            List<String> illegalOrphanMessages = null;
            if (employeeInfoOld != null && !employeeInfoOld.equals(employeeInfoNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain EmployeeInfo " + employeeInfoOld + " since its employeeid field is not nullable.");
            }
            for (EmployeeSchedule employeeScheduleListOldEmployeeSchedule : employeeScheduleListOld) {
                if (!employeeScheduleListNew.contains(employeeScheduleListOldEmployeeSchedule)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain EmployeeSchedule " + employeeScheduleListOldEmployeeSchedule + " since its employeeid field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (employeeInfoNew != null) {
                employeeInfoNew = em.getReference(employeeInfoNew.getClass(), employeeInfoNew.getId());
                employee.setEmployeeInfo(employeeInfoNew);
            }
            if (groupidNew != null) {
                groupidNew = em.getReference(groupidNew.getClass(), groupidNew.getId());
                employee.setGroupid(groupidNew);
            }
            List<EmployeeSchedule> attachedEmployeeScheduleListNew = new ArrayList<EmployeeSchedule>();
            for (EmployeeSchedule employeeScheduleListNewEmployeeScheduleToAttach : employeeScheduleListNew) {
                employeeScheduleListNewEmployeeScheduleToAttach = em.getReference(employeeScheduleListNewEmployeeScheduleToAttach.getClass(), employeeScheduleListNewEmployeeScheduleToAttach.getId());
                attachedEmployeeScheduleListNew.add(employeeScheduleListNewEmployeeScheduleToAttach);
            }
            employeeScheduleListNew = attachedEmployeeScheduleListNew;
            employee.setEmployeeScheduleList(employeeScheduleListNew);
            employee = em.merge(employee);
            if (employeeInfoNew != null && !employeeInfoNew.equals(employeeInfoOld)) {
                Employee oldEmployeeidOfEmployeeInfo = employeeInfoNew.getEmployeeid();
                if (oldEmployeeidOfEmployeeInfo != null) {
                    oldEmployeeidOfEmployeeInfo.setEmployeeInfo(null);
                    oldEmployeeidOfEmployeeInfo = em.merge(oldEmployeeidOfEmployeeInfo);
                }
                employeeInfoNew.setEmployeeid(employee);
                employeeInfoNew = em.merge(employeeInfoNew);
            }
            if (groupidOld != null && !groupidOld.equals(groupidNew)) {
                groupidOld.getEmployeeList().remove(employee);
                groupidOld = em.merge(groupidOld);
            }
            if (groupidNew != null && !groupidNew.equals(groupidOld)) {
                groupidNew.getEmployeeList().add(employee);
                groupidNew = em.merge(groupidNew);
            }
            for (EmployeeSchedule employeeScheduleListNewEmployeeSchedule : employeeScheduleListNew) {
                if (!employeeScheduleListOld.contains(employeeScheduleListNewEmployeeSchedule)) {
                    Employee oldEmployeeidOfEmployeeScheduleListNewEmployeeSchedule = employeeScheduleListNewEmployeeSchedule.getEmployeeid();
                    employeeScheduleListNewEmployeeSchedule.setEmployeeid(employee);
                    employeeScheduleListNewEmployeeSchedule = em.merge(employeeScheduleListNewEmployeeSchedule);
                    if (oldEmployeeidOfEmployeeScheduleListNewEmployeeSchedule != null && !oldEmployeeidOfEmployeeScheduleListNewEmployeeSchedule.equals(employee)) {
                        oldEmployeeidOfEmployeeScheduleListNewEmployeeSchedule.getEmployeeScheduleList().remove(employeeScheduleListNewEmployeeSchedule);
                        oldEmployeeidOfEmployeeScheduleListNewEmployeeSchedule = em.merge(oldEmployeeidOfEmployeeScheduleListNewEmployeeSchedule);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = employee.getId();
                if (findEmployee(id) == null) {
                    throw new NonexistentEntityException("The employee with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Employee employee;
            try {
                employee = em.getReference(Employee.class, id);
                employee.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The employee with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            EmployeeInfo employeeInfoOrphanCheck = employee.getEmployeeInfo();
            if (employeeInfoOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Employee (" + employee + ") cannot be destroyed since the EmployeeInfo " + employeeInfoOrphanCheck + " in its employeeInfo field has a non-nullable employeeid field.");
            }
            List<EmployeeSchedule> employeeScheduleListOrphanCheck = employee.getEmployeeScheduleList();
            //
            employeeScheduleListOrphanCheck = new ArrayList<EmployeeSchedule>();
            
            //
            for (EmployeeSchedule employeeScheduleListOrphanCheckEmployeeSchedule : employeeScheduleListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Employee (" + employee + ") cannot be destroyed since the EmployeeSchedule " + employeeScheduleListOrphanCheckEmployeeSchedule + " in its employeeScheduleList field has a non-nullable employeeid field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            EmployeeGroup groupid = employee.getGroupid();
            if (groupid != null) {
                groupid.getEmployeeList().remove(employee);
                groupid = em.merge(groupid);
            }
            em.remove(employee);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Employee> findEmployeeEntities() {
        return findEmployeeEntities(true, -1, -1);
    }

    public List<Employee> findEmployeeEntities(int maxResults, int firstResult) {
        return findEmployeeEntities(false, maxResults, firstResult);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	private List<Employee> findEmployeeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Employee.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Employee findEmployee(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Employee.class, id);
        } finally {
            em.close();
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	public int getEmployeeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Employee> rt = cq.from(Employee.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
